<script setup>
import { computed } from "vue";
import { useMenuStore, useUserStore } from "../store/index"; // 从统一入口引入
import { useRoute, useRouter } from "vue-router";
const menuStore = useMenuStore();
const userStore = useUserStore();
// 开/关 -- 切换侧边栏
const switch_on = () => {
  menuStore.toggleCollapse();
};
// 获得selectedMenu 的值显示在顶部菜单栏
const selectedMenulist = computed(() => {
  return menuStore.selectedMenu;
});
//
const route = useRoute();
const router = useRouter();

/**
 * 关闭tab页
 * @param {Object} item - 当前要关闭的tab页项
 * @param {Number} index - 当前要关闭的tab页项的索引
 */
const closeTablist = (item, index) => {
  // 调用menuStore的closeTab方法关闭tab页
  menuStore.closeTab(item);

  // 如果当前关闭的tab页不是当前路由对应的页面，则无需进一步操作
  if (item.path !== route.path) return;

  // 获取当前选中的菜单列表
  const closeIndex = selectedMenulist.value;

  // 判断当前选中的菜单列表长度是否与当前要关闭的tab页项索引相同
  if (closeIndex.length === index) {
    // 如果当前选中的菜单列表为空，则跳转到根路径
    if (!closeIndex.length) {
      router.push("/");
    } else {
      // 否则跳转到当前选中的菜单列表中前一个路径
      router.push(closeIndex[index - 1].path);
    }
  } else {
    // 如果当前选中的菜单列表长度不等于当前要关闭的tab页项索引，则跳转到当前选中的菜单列表中当前索引对应的路径
    router.push(closeIndex[index].path);
  }
};
// 退出登录
const handleQuit = () => {
  console.log("退出登录");
  // 确认退出登录
  ElMessageBox.confirm("确认退出登录吗？", "提示", {
    confirmButtonText: "确定",
    cancelButtonText: "取消",
    type: "warning",
  })
    .then(() => {
      // 删除token/用户信息
      userStore.clearToken();
      // 跳转到登录页面
      router.push("/login");
      // 执行退出登录操作
      ElMessage({
        type: "success",
        message: "退出登录成功",
      });
    })
    .catch(() => {
      ElMessage({
        type: "info",
        message: "已取消退出登录",
      });
    });
};
</script>

<template>
  <div class="header-container">
    <div class="header-left flex-box">
      <el-icon size="20" class="icon" @click="switch_on"><Fold /></el-icon>
      <ul class="flex-box">
        <li
          v-for="(item, index) in selectedMenulist"
          :key="item.path"
          :class="{ active: item.path === route.path }"
          class="flex-box tab"
        >
          <el-icon size="12">
            <component :is="item.icon" />
          </el-icon>
          <router-link :to="item.path" class="flex-box text">
            <span class="tab-name">{{ item.name }}</span>
          </router-link>
          <el-icon size="12" class="close" @click="closeTablist(item, index)">
            <Close />
          </el-icon>
        </li>
      </ul>
    </div>
    <div class="header-right">
      <el-dropdown>
        <div class="el-dropdown-link flex-box">
          <el-avatar
            src="https://cube.elemecdn.com/0/88/03b0d39583f48206768a7534e55bcpng.png"
          />
          <p class="user-name">用户名</p>
        </div>
        <template #dropdown>
          <el-dropdown-menu @click="handleQuit">
            <el-dropdown-item>退出</el-dropdown-item>
          </el-dropdown-menu>
        </template>
      </el-dropdown>
    </div>
  </div>
</template>

<style lang="less" scoped>
.flex-box {
  display: flex;
  align-items: center;
  height: 100%;
}
.header-container {
  display: flex;
  justify-content: space-between;
  align-items: center;
  height: 100%;
  background-color: #fff;
  padding-right: 25px;
  .header-left {
    height: 100%;
    .icon {
      width: 45px;
      height: 100%;
    }
    .icon:hover {
      background-color: #f5f5f5;
      cursor: pointer;
    }
    .tab {
      padding: 10px;
      height: 100%;

      .text {
        color: #000;
        margin: 0 5px;
      }
      .close {
        visibility: hidden;
      }
      &.active {
        a {
          color: #409eff;
        }
        i {
          color: #409eff;
        }
        background-color: #f5f5f5;
      }
    }
    .tab:hover {
      background-color: #f5f5f5;
      .close {
        visibility: inherit;
        cursor: pointer;
        color: #000;
      }
    }
  }
  .header-right {
    .user-name {
      margin-left: 10px;
    }
  }
}
</style>
